package com.sitech.portal.system.dao.ibatis;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.sitech.ibnms.core.BaseIbnmsDao;
import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.PageForm;
import com.sitech.ibnms.util.RandomGUID;
import com.sitech.portal.system.dao.ISysPostTimeDao;
import com.sitech.portal.system.domain.TblSYS_POSTTIME;
import org.apache.commons.lang.StringUtils;

public class SysPostTimeDaoImpl extends BaseIbnmsDao implements ISysPostTimeDao {

	public String add(TblSYS_POSTTIME tblSYS_POSTTIME) {
		tblSYS_POSTTIME.setID(RandomGUID.getRandomGUID());
		tblSYS_POSTTIME.setEND_TIME(null);
		this.insert("insertForTblSYS_POSTTIME", tblSYS_POSTTIME);
		return tblSYS_POSTTIME.getID();

	}

	public void update(TblSYS_POSTTIME tblSYS_POSTTIME) {
		//HashMap map = new HashMap();
		//map.put("END_TIME", new Date());
		//map.put("ID", tblSYS_POSTTIME.getID());
		tblSYS_POSTTIME.setID(RandomGUID.getRandomGUID());
		tblSYS_POSTTIME.setEND_TIME(new Date());
		//this.update("updateForTblSYS_POSTTIME", tblSYS_POSTTIME);
	}

	public void delete(TblSYS_POSTTIME tblSYS_POSTTIME) {
		this.delete("deleteForTblSYS_POSTTIME", tblSYS_POSTTIME);
	}

	/**
	 * user security log list(post time)
	 */
	public IPage findPostTimeList(PageForm pageForm) {
		return this.findPage("getForTblSYS_POSTTIME", pageForm);
	}

	@Override
	public IPage getPostTimeReport(String beginTime,String endTime,String queryRule,PageForm pageForm) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Map<String,Object> param=new HashMap<String,Object>();
		param.put("beginTime",beginTime);
		if(StringUtils.isNotEmpty(endTime)){
			Calendar calendar=Calendar.getInstance();
			try {
				calendar.setTime(simpleDateFormat.parse(endTime));
				calendar.add(Calendar.DAY_OF_MONTH,1);
				endTime=simpleDateFormat.format(calendar.getTime());
			} catch (ParseException e) {
				throw new RuntimeException("时间转换出错");
			}
		}
		param.put("endTime",endTime);
		param.put("queryRule",queryRule);
		return this.findPage("getUserLoginReport_POSTTME",param,pageForm);
	}

	@Override
	public IPage getNonLoginUser(String beginTime, String endTime, String queryRule, PageForm pageForm) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
		Map<String,Object> param=new HashMap<String,Object>();
		param.put("beginTime",beginTime);
		if(StringUtils.isNotEmpty(endTime)){
			Calendar calendar=Calendar.getInstance();
			try {
				calendar.setTime(simpleDateFormat.parse(endTime));
				calendar.add(Calendar.MONTH,1);
				endTime=simpleDateFormat.format(calendar.getTime());
			} catch (ParseException e) {
				throw new RuntimeException("时间转换出错");
			}
		}
		param.put("endTime",endTime);
		param.put("queryRule",queryRule);
		return this.findPage("queryUserNotLogin",param,pageForm);
	}

	@Override
	public IPage getPostTimeListByFilter(String loginId, String beginTime, String endTime, String queryRule, PageForm pageForm) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Map<String,Object> param=new HashMap<String,Object>();
		param.put("beginTime",beginTime);
		param.put("loginId",loginId);
		if(StringUtils.isNotEmpty(endTime)){
			Calendar calendar=Calendar.getInstance();
			try {
				calendar.setTime(simpleDateFormat.parse(endTime));
				calendar.add(Calendar.DAY_OF_MONTH,1);
				endTime=simpleDateFormat.format(calendar.getTime());
			} catch (ParseException e) {
				throw new RuntimeException("时间转换出错");
			}
		}
		param.put("endTime",endTime);
		param.put("queryRule",queryRule);
		return this.findPage("queryPostTimeListByFilter",param,pageForm);
	}
}
